<template>
  <div>
    <el-card class="mt-10" shadow="hover">
      <el-form>
        <el-form-item label="属性名">
          <el-input
            v-model="attrFormData.attrName"
            placeholder="请输入属性名"
            class="attr-inp"
          />
        </el-form-item>
      </el-form>
      <el-form>
        <el-button
          type="primary"
          :icon="Plus"
          :disabled="!attrFormData.attrName"
          @click="addAttrValue"
        >
          添加属性值
        </el-button>
        <el-table :data="attrFormData.attrValueList" border class="mt-20">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80"
          />
          <el-table-column label="属性名称">
            <template v-slot="{ row, $index }">
              <el-input
                placeholder="请输入属性名"
                size="small"
                ref="inputRef"
                v-if="row.isShowEdit"
                @blur="switchShow(row, $index)"
                v-model="row.valueName"
              />
              <div v-else @click="switchEdit(row)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-popconfirm
                :title="`确定要删除 ${row.valueName} 嘛?`"
                @confirm="deleteAttrValue($index)"
              >
                <template #reference>
                  <el-button type="danger" :icon="Delete" size="small" />
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <el-button
        type="primary"
        class="mt-20"
        :disabled="isShowSaveBtn"
        @click="addOrUpdateAttr"
      >
        保存
      </el-button>
      <el-button @click="cancel" class="mt-20">取消</el-button>
    </el-card>
  </div>
</template>

<script lang="ts">
export default {
  name: "AddOrUpdateAttr",
};
</script>

<script lang="ts" setup>
import { useCategoryStore } from "@/stores/category";
import { useAttrStore } from "@/stores/attr";
import { Plus, Delete } from "@element-plus/icons-vue";
import { reactive, ref, nextTick, computed } from "vue";
import { addOrUpdataAttrApi } from "@/api/product/attr";
import type { AttrItem, AttrValueItem } from "@/api/product/model/attrModel";
import { ElMessage } from "element-plus";

const attrStore = useAttrStore();
// 表单数据
const attrFormData = reactive<AttrItem>({
  // attrName: "",
  // attrValueList: [],
  attrName: attrStore.attrItem.attrName,
  attrValueList: attrStore.attrItem.attrValueList,
});

const inputRef = ref();
const addAttrValue = () => {
  attrFormData.attrValueList.push({
    isShowEdit: true,
    valueName: "",
  });
  nextTick(() => {
    inputRef.value.focus();
  });
};

// const isShowEdit = ref(true);

// 失去焦点变成显示模式
const switchShow = (row: AttrValueItem, index: number) => {
  // 删除整行数据
  if (!row.valueName) {
    attrFormData.attrValueList.splice(index, 1);
  }
  row.isShowEdit = false;
};

// 点击变成编辑模式
const switchEdit = async (row: AttrValueItem) => {
  row.isShowEdit = true;
  // nextTick(() => {
  //   inputRef.value.focus();
  // });
  await nextTick();
  inputRef.value.focus();
};

// 删除属性值
const deleteAttrValue = (index: number) => {
  attrFormData.attrValueList.splice(index, 1);
};

// 显示隐藏保存按钮
const isShowSaveBtn = computed(() => {
  // 想要显示保存按钮需满足以下条件
  // 1.属性名有值
  // 2.属性值列表中至少有一个属性值名称
  return !(
    attrFormData.attrName &&
    attrFormData.attrValueList.some((item) => item.valueName)
  );
});

const categoryStore = useCategoryStore();

// 点击添加或修改
const addOrUpdateAttr = async () => {
  const { attrName, attrValueList } = attrFormData;
  const { category3Id } = categoryStore;
  // 如果有id就是修改数据
  const { id } = attrStore.attrItem;
  await addOrUpdataAttrApi({
    attrName,
    attrValueList,
    categoryLevel: 3,
    categoryId: category3Id,
    id,
  });
  ElMessage.success(`${id ? "更新" : "添加"}属性成功 `);
  cancel();
};

const emit = defineEmits(["setIsShowAttrList"]);
const cancel = () => {
  emit("setIsShowAttrList", true);
};
</script>

<style scoped>
.attr-inp {
  width: 200px;
}
</style>
